<?php
//用户类
class User_model extends CI_Model{

    public function __construct(){

        parent::__construct();
        $this->load->database();
    }

    //登陆界面方法
    public function validUserLogin($username, $password){
        //先按工号查询是否有这个用户
        // $this->db->select('*');
        // $this->db->where('name', $username);
        // $query = $this->db->get('dorm_user');
        $query_user = $this->db->get_where('dorm_user' ,array('username' => $username));

        //若查询用户成功则判断密码是否正确
        if ($query_user->num_rows() > 0) {

            // $where = array('name' => $username, 'password' => $password);
            // $this->db->select('*');
            // $this->db->where($where);
            // $query_password = $this->db->get('dorm_user');
            $query_password = $this->db->get_where('dorm_user' ,array('username' => $username ,'password' => $password));
            //按账号密码进行查询，若取出行数大于0则证明密码正确
            if ($query_password->num_rows() > 0) {
                //查询成功 
                $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $query_user->row()->username);
                $result = json_encode($return_su);
            } else {
                //查询失败
                $return_fa = array('res' => '40001', 'msg' => '密码错误', 'data' => '');
                $result = json_encode($return_fa);
            }
        } else {
            //查询用户失败返回用户不存在
            $return_fa = array('res' => '40001', 'msg' => '用户不存在', 'data' => '');
            $result = json_encode($return_fa);
        }

        return $result;
    }

    //用户密码修改
    public function  password_update($username, $password){
        //去用户的新密码进行更新
        $data = array('password' => $password);
        $where = array('username' => $username);
        $this->db->where($where);
        $this->db->update('dorm_user', $data);
        $query_password = $this->db->affected_rows();
        //如果所影响行数为true则修改成功
        if ($query_password) {
            //修改成功
            $return_su = array('res' => '40000', 'msg' => '密码修改成功' ,'data' => '');
            $result = json_encode($return_su);
        } else {
            //修改失败
            $return_fa = array('res' => '40001', 'msg' => '密码修改失败' , 'data' => '');
            $result = json_encode($return_fa);
        }
        return $result;
    }

    //用户账号注销
    public function user_delete($username ,$admname){
        //只有管理员账号才能删除账号
        if($admname == 'A001'){
            $this->db->delete('dorm_user' ,array('username' => $username));
            $query_user = $this->db->affected_rows();
            if($query_user){
                //删除成功
                $return_su = array('res' => '40000', 'msg' => '账号删除成功' ,'data' => '');
                $result = json_encode($return_su);
            }else{
                //删除失败
                $return_fa = array('res' => '40001', 'msg' => '密码修改失败' ,'data' => '');
                $result = json_encode($return_fa);
            }
        }else{
            //删除失败
            $return_fa = array('res' => '40001', 'msg' => '无权限删除账号' ,'data' => '');
            $result = json_encode($return_fa);
        }
        return $result;
    }
}
